package com.tsin.usercenter.once;

import com.tsin.usercenter.mapper.UserMapper;
import com.tsin.usercenter.model.User;
import com.tsin.usercenter.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
public class InserUsers {
    @Resource
    private UserService userService;
//    @Scheduled(initialDelay = 5000,fixedRate = Long.MAX_VALUE)
    public void doInsertUsers(){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        final int NUM = 10000;   //插入数据数量
        final int THREAD_NUM = 10; //线程数量
        int j = 0;
        List<CompletableFuture<Void>> futureList = new ArrayList<>();
        for (int i = 0; i < THREAD_NUM; i++) {
            ArrayList<User> userArrayList = new ArrayList<>();
            while(true){
                j++;
                User user = new User();
                user.setNickName("模拟用户"+j);
                user.setUserAccount("Ytest"+j);
                user.setAvatarUrl("https://img0.baidu.com/it/u=3661287254,3359322337&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=705");
                user.setGender(0);
                user.setPassword("3e8c6a697a79f17d78db5efd95f6809d");
                user.setPhone("+86 1*****");
                user.setEmail("tsin09@qq.com");
                user.setUserStatus(0);
                user.setCreateTime(new Date());
                user.setUpdateTime(new Date());
                user.setIsDelete(0);
                user.setUserRole(0);
                user.setTags("[\"男\"]");
                userArrayList.add(user);
                if(j%1000==0){
                    break;
                }
            }
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                userService.saveBatch(userArrayList, 1000);
            });
            futureList.add(future);
        }
        CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join();
        stopWatch.stop();
        System.out.println("花费时间:"+stopWatch.getTotalTimeMillis());

    }
}